//
//  RMLTabBar.swift
//  weibo
//
//  Created by blue on 16/4/18.
//  Copyright © 2016年 blue. All rights reserved.
//

import UIKit
//协议
protocol RMLTabBarDelegate: NSObjectProtocol{

//协议方法
    func compButtonClick()
}

class RMLTabBar: UITabBar {

//代理属性
    weak var RMLDelegate:RMLTabBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
//MARK: - 设置控件
    func setUpUI(){
    
        addSubview(composeButton)
    }

//MARK: - 设置控件的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = KWIDTH / 5
        var index:CGFloat = 0
        
        //遍历子控件
        for value in self.subviews{
        
        //判断子控件是否是需要的UITabBarButton
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
            //设置UItabBarButton位置及大小
                value.frame.origin.x = index * width
                value.frame.size.width = width
            
            index++
                
            //判断
                if index == 2{
                index++
                }
            }
        }
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
    }

//MARK: - 设置中间的按钮
private lazy var composeButton: UIButton = {

   let compButton = UIButton()
    
    //设置背景图片
    compButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    compButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    //设置图片
    compButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    compButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    compButton.sizeToFit()
    //添加点击事件
    compButton.addTarget(self, action: "compButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    return compButton
}()
    
//MARK: - compButton点击事件
  @objc private func compButtonClick(){
    
    RMLDelegate?.compButtonClick()
    
    }
    
}